/*
 * mixin list
 */
import PageList from '@/components/PageList'
import ListTable from '@/components/ListTable'
import { mapGetters } from 'vuex'
const mixin = {
  components: {
    PageList, // 分页
    ListTable // 列表
  },
  data() {
    return {
      // 列表数据
      tableData: [],
      // 是否开启loading
      tableLoading: false
    }
  },
  computed: {
    ...mapGetters(['serchObj'])
  },
  watch: {
    async serchObj(newValue) {
      let keyList = Object.keys(newValue)
      console.info(23323232323, newValue)
      if (keyList.length > 0) {
        // 路由改变时查询
        await this.$_setQuery(newValue)
      }
    }
  },
  // 进入页面查询列表
  mounted() {
    this.$store.dispatch('user/setSerchObj', { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false })
    // 查询
    if (this.title === '考勤统计') {
      this.getList()
    }
  },
  methods: {
    getList() {
      // 开启 loading
      this.tableLoading = true
      // 关闭 loading
      this.tableLoading = false
    },
    // 设置路由，也就是查询参数
    async $_setQuery(query) {
      let obj = query || { pageIndex: 1, pageSize: 10 }
      obj.orderBy = 'createdTime'
      obj.orderByAscent = false
      await this.getList(obj)
    },
    // 组件的查询按钮
    async handleSearch(search) {
      await this.$store.dispatch('user/setSerchObj', search)
    },
    // 组件的重置按钮
    async handleReset(search) {
      await this.$store.dispatch('user/setSerchObj', search)
    },
    busRefreshList() {
      const { page = 1 } = this.serchObj
      if (page === 1) {
        this.getList()
      } else {
        this.$_setQuery({ page: 1 })
      }
    },
    // 删除操作
    $_delConfirm(msg, yesCb, noCb) {
      this.$confirm(`此操作将永久删除${msg}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          yesCb && yesCb()
        })
        .catch(() => {
          noCb && noCb()
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    $_showConfirm(msg, yesCb, noCb) {
      this.$confirm(`${msg}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          yesCb && yesCb()
        })
        .catch(() => {
          noCb && noCb()
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    }
  }
}
export default mixin
